<?php
/**
 * @name Bootstrap
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends Yaf_Bootstrap_Abstract {
	private $_config;
	
    public function _initConfig() {
		//把配置保存起来
		$this->_config = $arrConfig = Yaf_Application::app()->getConfig();
		Yaf_Registry::set('config', $arrConfig);
		
	}
	
	/*
	 * initIncludePath is only required because zend components have a shit load of
	 * include_once calls everywhere. Other libraries could probably just use
	 * the autoloader (see _initNamespaces below).
	 */
	public function _initIncludePath(){
		set_include_path(get_include_path() . PATH_SEPARATOR . $this->_config->application->library);
	}
	
	public function _initErrors(){
		if($this->_config->application->showErrors){
			error_reporting (-1);
			ini_set('display_errors','On');
		}
	}
	
	public function _initNamespaces(){
		Yaf_Loader::getInstance()->registerLocalNameSpace(array("Zend"));
	}
	
	public function _initRoutes(){
	
		//this does nothing useful but shows the regex router in action...
		Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
		"paging_example",
		new Yaf_Route_Regex(
		"#^/index/page/(\d+)#",
		array('controller' => "index"),
		array(1 => "page")
		)
		);
	
	}
	
	public function _initLayout(Yaf_Dispatcher $dispatcher){
		/*layout allows boilerplate HTML to live in /views/layout rather than every script*/
	//$layout = new LayoutPlugin('layout.phtml');
	
		/* Store a reference in the registry so values can be set later.
		 * This is a hack to make up for the lack of a getPlugin
		 * method in the dispatcher.
		*/
		//Yaf_Registry::set('layout', $layout);
	
		/*add the plugin to the dispatcher*/
		//$dispatcher->registerPlugin($layout);
	}
	
	public function _initDefaultDbAdapter(){
		//$ttt=$this->_config->database->params->toArray();
		
		$dbAdapter = new Zend_Db_Adapter_Sqlsrv(
				$this->_config->database->params->toArray()
		);
	
		Zend_Db_Table::setDefaultAdapter($dbAdapter);
	}

	public function _initPlugin(Yaf_Dispatcher $dispatcher) {
		//注册一个插件
		$objSamplePlugin = new SamplePlugin();
		$dispatcher->registerPlugin($objSamplePlugin);
	}

	public function _initRoute(Yaf_Dispatcher $dispatcher) {
		
		//在这里注册自己的路由协议,默认使用简单路由
		$router = $dispatcher->getRouter();
		$router->addConfig(Yaf_Registry::get("config")->routes);
	}
	
	public function _initView(Yaf_Dispatcher $dispatcher) {
		//在这里注册自己的view控制器，例如smarty,firekylin
		Yaf_Dispatcher::getInstance()->autoRender(false);
	}
}
